package com.ruoyi.web.controller.system;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工号管理Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/employeeId")
public class SysEmployeeIdController extends BaseController
{
    @Autowired
    private ISysUserService userService;

    /**
     * 获取可用工号列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/available")
    public AjaxResult getAvailableEmployeeIds()
    {
        List<String> employeeIds = userService.getAvailableEmployeeIds();
        return success(employeeIds);
    }

    /**
     * 回收已删除用户的工号
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "工号管理", businessType = BusinessType.UPDATE)
    @PutMapping("/recycle/{employeeId}")
    public AjaxResult recycleEmployeeId(@PathVariable String employeeId)
    {
        int result = userService.recycleEmployeeId(employeeId);
        if (result > 0)
        {
            return success("工号回收成功");
        }
        else
        {
            return error("工号回收失败，可能该工号不存在或未被删除");
        }
    }

    /**
     * 批量回收已删除用户的工号
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "工号管理", businessType = BusinessType.UPDATE)
    @PutMapping("/recycle/batch")
    public AjaxResult batchRecycleEmployeeIds(@RequestBody List<String> employeeIds)
    {
        int totalRecycled = 0;
        for (String employeeId : employeeIds)
        {
            int result = userService.recycleEmployeeId(employeeId);
            if (result > 0)
            {
                totalRecycled++;
            }
        }
        
        if (totalRecycled > 0)
        {
            return success("成功回收 " + totalRecycled + " 个工号");
        }
        else
        {
            return error("没有工号被回收");
        }
    }
}
